using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using System.Threading;

using MULENetworking;
//using MainGame;

namespace MainGame
{
    public partial class PlayerCreation : Form
    {
        private int avatar;
        private Color pColor;
        private string name;
        private IRemoteServer remoteServer;
        private IGame game;
        private ArrayList readyList;
        private int selected = -1;
        private Thread thread;
        private string NetInfo;

        public PlayerCreation(IRemoteServer server, string haxoralias, IGame gam, string serverInfo)
        {
            InitializeComponent();
            remoteServer = server;
            name = haxoralias;
            game = gam;
            avatar = -1;
            readyList = new ArrayList();
            NetInfo = serverInfo;
            thread = new Thread(new ThreadStart(PollingService));
            thread.Start();
        }


        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            avatar = 2;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            avatar = 1;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            avatar = 3;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            avatar = 4;
        }

        private void PlayerCreation_Load(object sender, EventArgs e)
        {

        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            avatar = 6;
        }

        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
            avatar = 7;
        }

        private void radioButton8_CheckedChanged(object sender, EventArgs e)
        {
            avatar = 8;
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            avatar = 5;
        }

        private void ReadyButton_Click(object sender, EventArgs e)
        {
            if (name == null || avatar == null)
            {
                //do nothing;
            }
            else
            {
                //chatServer.CreatePlayer(name, avatar); DEPRECATED
                remoteServer.CreatePlayer(name, game, avatar, pColor);
                remoteServer.CurrentGame = game;
                ReadyButton.Text = "Waiting for other players.";
                ReadyButton.Enabled = false;
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
         
        }

        private void FillListBox1()
        {
            int selected = listBox1.SelectedIndex;
            listBox1.Items.Clear();
            foreach (string playerName in readyList)
            {   
                listBox1.Items.Add(playerName);
            }
            if (listBox1.Items.Count > selected)
            {
                listBox1.SelectedIndex = selected;
            }
        }

        private void PollingService()
        {
            ArrayList tempList = null;
            
            while (true)
            {
                Thread.Sleep(1000);

                tempList = remoteServer.PlayersList;
                if(readyList.Count == tempList.Count)
                {
                    Thread.Sleep(1000);
                }
                else
                {
                    readyList = tempList;

                    if (listBox1.InvokeRequired)
                    {
                        try
                        {
                            listBox1.Invoke(new MethodInvoker(FillListBox1), null);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.InnerException.Message);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Polling service is running in the UI thread for some reason...");
                        Console.WriteLine("listBox1 did not require an invoke, and was not updated.");
                    }
                }
                if (listBox1.Items.Count == 4) break;
            }
            if (listBox1.Items.Count == 4)
            {
                // start gameflow

                // start the game
                int thisPlayer = -1;
                if (remoteServer.CurrentGame.Player1.name.Equals(name))
                {
                    thisPlayer = 1;
                }
                else if (remoteServer.CurrentGame.Player2.name.Equals(name))
                {
                    thisPlayer = 2;
                }
                else if (remoteServer.CurrentGame.Player3.name.Equals(name))
                {
                    thisPlayer = 3;
                }
                else if (remoteServer.CurrentGame.Player4.name.Equals(name))
                {
                    thisPlayer = 4;
                }
                //Application.EnableVisualStyles();
                //Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainGame(NetInfo, thisPlayer));
                thread.Abort();
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (thread != null)
            {
                thread.Abort();
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (color.Text)
            {
                case "White":
                    pColor = Color.White;
                    break;
                case "Purple":
                    pColor = Color.Purple;
                    break;
                case "Blue":
                    pColor = Color.Blue;
                    break;
                case "Red":
                    pColor = Color.Red;
                    break;
                case "Orange":
                    pColor = Color.Orange;
                    break;
                default:
                    pColor = Color.White;
                    break;
            }
        }
    }
}